package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.RackDao;
import com.bocloud.cmp.entity.resource.Rack;
import com.bocloud.cmp.model.Scope;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 机柜抽象DAO接口实现类
 * 
 * @author wei wei
 * @version 1.0
 * @since 2016.02.29
 */
@Repository("rackDao")
public class RackDaoImpl extends JdbcGenericDao<Rack, Long> implements RackDao {

	@Override
	public Rack query(Long id) throws Exception {
		String sql = "select a.*,b.name roomname from rack a left join room b on a.room_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Rack> list = this.list(Rack.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Rack> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,b.name roomname,c.name dcname from rack a left join room b on a.room_id = b.id left join data_center c on b.dc_id = c.id where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(Rack.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from rack a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Rack> racks = this.list(Rack.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (Rack rack : racks) {
			beans.add(new SimpleBean(rack.getId(), rack.getName()));
		}
		return beans;
	}

	@Override
	public boolean releaseRoom(Long id, Long userId) throws Exception {
		String sql = "update rack set room_id = null , gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public boolean belongRoom(Long id, Long roomId, Long userId) throws Exception {
		String sql = "update rack set room_id = :roomId , gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("roomId", roomId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Rack> listFree() throws Exception {
		String sql = "select * from rack where is_deleted = 0 and room_id is null and rack_id is null";
		return this.list(Rack.class, sql);
	}

	@Override
	public List<Rack> listFree(Long roomId) throws Exception {
		String sql = "select * from rack where is_deleted = 0 and (room_id = :roomId or room_id is null)";
		Map<String, Object> params = MapTools.simpleMap("roomId", roomId);
		return this.list(Rack.class, sql, params);
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from rack a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update rack set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public boolean exists(List<Param> params) throws Exception {
		String sql = "select * from rack a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Rack> list = this.list(Rack.class, sql, paramMap);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	@Override
	public Scope range(Long roomId) throws Exception {
		String sql = "select max(a.axis_x) rowNum,max(a.axis_y) colNum from rack a where a.is_deleted = 0 and a.room_id = :roomId";
		Map<String, Object> params = MapTools.simpleMap("roomId", roomId);
		List<Map<String, Object>> list = this.list(sql, params);
		if (list.isEmpty()) {
			return null;
		}
		Map<String, Object> map = list.get(0);
		return new Scope(Integer.parseInt(map.get(Common.ROW).toString()),
				Integer.parseInt(map.get(Common.COL).toString()));
	}
}
